﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using Dashboard.Expense.Business;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win;

namespace Dashboard.Expense
{
    public partial class FormExpenseLimit : Form
    {
        public FormExpenseLimit()
        {
            InitializeComponent();
        }

        private void FormUnproductiveTime_Load(object sender, EventArgs e)
        {
            this.LoadAppTypes();
        }

        private void LoadAppTypes()
        {
            try
            {
                Exp_ExpenseType eet = new Exp_ExpenseType();
                DataTable types = eet.QueryApplicationTypes();

                this.comboBoxApplicationType.Items.Clear();
                this.comboBoxApplicationType.Items.Add(new ComboBoxItem() { Text = "", Value = "" });
                foreach (DataRow row in types.Rows)
                {
                    this.comboBoxApplicationType.Items.Add(new ComboBoxItem() { Text = row["Code"].ToString(), Value = row["ID"].ToString() });
                }
                this.comboBoxApplicationType.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                
            }
        }

        private void LoadExpenses()
        {
            if (this.comboBoxApplicationType.Text == "")
            {
                this.comboBoxExpenseType.Items.Clear();
                this.comboBoxExpenseType.Items.Add("");
            }
            else
            {
                DataTable expenses = new Exp_ExpenseType().QueryExpenseTypes(int.Parse(((ComboBoxItem)this.comboBoxApplicationType.SelectedItem).Value.ToString()));
                this.comboBoxExpenseType.Items.Clear();
                this.comboBoxExpenseType.Items.Add("");

                foreach (DataRow row in expenses.Rows)
                {
                    this.comboBoxExpenseType.Items.Add(row["Code"].ToString());
                }
            }
            this.comboBoxExpenseType.SelectedIndex = 0;
        }

        private void toolStripButtonQuery_Click(object sender, EventArgs e)
        {
            QueryData();
        }

        private void QueryData()
        {
            try
            {
                Exp_ExpenseLimit eel = new Exp_ExpenseLimit();
                DataTable limits = eel.QueryExpenseLimits(this.comboBoxApplicationType.Text, this.comboBoxExpenseType.Text);
                this.ultraGridMain.DataSource = limits;
                this.ultraGridMain.DataBind();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                
            }
        }

        private void toolStripButtonCreate_Click(object sender, EventArgs e)
        {
            using (FormAddExpenseLimit addForm = new FormAddExpenseLimit())
            {
                addForm.ShowDialog(this);

                this.QueryData();
            }
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            this.ultraGridMain.UpdateData();

            List<int> selectedIds = new List<int>();
            foreach (UltraGridRow row in this.ultraGridMain.Rows)
            {
                if (Convert.ToBoolean(row.Cells["Check"].Value) == true)
                {
                    selectedIds.Add(Convert.ToInt32(row.Cells["Id"].Value));
                }
            }

            if (selectedIds.Count == 0)
            {
                MessageBox.Show(this, "No data selected.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (MessageBox.Show(this, "Are you sure to delete selected records?", "Confirm",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                try
                {
                    this.Cursor = Cursors.WaitCursor;

                    Exp_ExpenseLimit eel = new Exp_ExpenseLimit();
                    foreach (int id in selectedIds)
                    {
                        eel.DeleteExpenseLimit(id);
                    }

                    MessageBox.Show(this, "Delete successfully.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    QueryData();
                    this.Cursor = Cursors.Default;
                }
            }
        }

        private void ultraGridMain_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            // 自适应列宽
            e.Layout.AutoFitStyle = Infragistics.Win.UltraWinGrid.AutoFitStyle.None;
            // 是否允许调整列宽
            e.Layout.Override.AllowColSizing = AllowColSizing.Free;
            // 是否允许移动栏位顺序
            e.Layout.Override.AllowColMoving = AllowColMoving.NotAllowed;

            // 设置Grid的Split窗口个数，建议设置为1--不允许Split
            e.Layout.MaxColScrollRegions = 1;
            e.Layout.MaxRowScrollRegions = 1;

            // 冻结列
            e.Layout.UseFixedHeaders = false;

            // 排序
            e.Layout.Override.HeaderClickAction = Infragistics.Win.UltraWinGrid.HeaderClickAction.SortSingle;

            // 不允许删除
            e.Layout.Override.AllowDelete = DefaultableBoolean.False;

            // 滚动提示
            e.Layout.Bands[0].ScrollTipField = "AppTypeCode";

            // 隐藏不需要显示的列
            e.Layout.Bands[0].Columns["ID"].Hidden = true;

            for (int i = 0; i < e.Layout.Bands[0].Columns.Count; i++)
            {
                if (e.Layout.Bands[0].Columns[i].Key != "Check")
                {
                    e.Layout.Bands[0].Columns[i].CellActivation = Activation.NoEdit;
                }
                else
                {
                    e.Layout.Bands[0].Columns[i].CellActivation = Activation.AllowEdit;
                }
            }
        }

        private void toolStripButtonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBoxApplicationType_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                this.LoadExpenses();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                
            }
        }

        private void ultraGridMain_ClickCellButton(object sender, CellEventArgs e)
        {
            if (e.Cell.Column.Key == "Edit")
            {
                int id = int.Parse(e.Cell.Row.Cells["ID"].Value.ToString());

                using (FormAddExpenseLimit editForm = new FormAddExpenseLimit())
                {
                    editForm.ID = id;

                    editForm.ShowDialog(this);

                    this.QueryData();
                }
            }
        }
    }
}
